﻿using AlarmClock.BLL;
using Model;
using System;
using System.Drawing;
using System.Windows.Forms;
using static AlarmClock.BLL.Service;

namespace AlarmClock.UI.Controls
{
    public partial class Clock_Ctrl : UserControl
    {
        public ChangeClockHandler ChangeClock { get; set; }

        private ClockBLL clockBLL = new ClockBLL();

        public Clock_Ctrl(Clock clock)
        {
            InitializeComponent();

            this.Tag = clock;
            this.Name = clock.ClockName;

            //时间
            string time = clock.clockInfo.time;
            this.Hour_Ten.Text = time.Substring(11, 1);
            this.Hour_S.Text = time.Substring(12, 1);
            this.Minute_Ten.Text = time.Substring(14, 1);
            this.Minute_S.Text = time.Substring(15, 1);

            //重复+备注：
            this.RepeatAndThings.Text = clock.clockInfo.repeat + " | " + clock.clockInfo.things;
            //激活状态
            this.check.Checked = clock.clockInfo.isActive == "1" ? true : false;
            UpdateUIBackColor();

            this.MouseClick += Clock_Ctrl_Click;    
            this.MouseHover += (s1, e1) => { this.BackColor = Color.LightSteelBlue; this.BorderStyle = BorderStyle.FixedSingle; };
            this.MouseLeave += (s1, e1) => { UpdateUIBackColor(); this.BorderStyle = BorderStyle.None; };

            this.toolTip.SetToolTip(this, $"点击编辑闹钟\r\n{clock.clockInfo.time}");
        }

        /// <summary>
        /// 控件单击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void check_Click(object sender, EventArgs e)
        {
            Clock curClock = this.Tag as Clock;

            //更新保存的数据文件
            if (this.check.Checked) curClock.clockInfo.isActive = "1";
            else curClock.clockInfo.isActive = "0";

            OnChangeClock(curClock, Constant.CLOCK_UPDATE);
        }

        private void Clock_Ctrl_Click(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left) return;

            FrmClockUpdate frmClockUpdate = new FrmClockUpdate((Clock)(sender as Clock_Ctrl).Tag, "修改闹钟");
            frmClockUpdate.ChangeClock += new ChangeClockHandler(OnChangeClock);
            frmClockUpdate.ShowDialog();
        }

        /// <summary>
        /// 修改闹钟：下一层控件注册了委托，也会触发该函数
        /// </summary>
        /// <param name="clock"></param>
        /// <param name="order"></param>
        private void OnChangeClock(Clock clock, string order)
        {
            ChangeClock?.Invoke(clock, order);//交给主界面去更新闹钟控件 和 配置文件
        }

        /// <summary>
        /// 【打开/关闭】：改变标签颜色 和 控件背景色
        /// </summary>
        private void UpdateUIBackColor()
        {
            if (!this.check.Checked)
            {
                this.label1.ForeColor = Hour_Ten.ForeColor = Hour_S.ForeColor = Minute_Ten.ForeColor = Minute_S.ForeColor
                    = Color.FromKnownColor(KnownColor.AppWorkspace);
                this.BackColor = Color.FromKnownColor(KnownColor.Control);
            }
            else
            {
                this.label1.ForeColor = Hour_Ten.ForeColor
                    = Hour_S.ForeColor = Minute_Ten.ForeColor = Minute_S.ForeColor = Color.Black;
                this.BackColor = Color.White;
            }
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定要删除当前选中闹钟吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (result == DialogResult.Cancel) return;

            //执行回调，删除主界面panelMain里面的记录
            ChangeClock?.Invoke((Clock)this.Tag, Constant.CLOCK_DELETE);
        }

    }
}
